package com.appsky.drift.api;

import com.alibaba.fastjson.JSONObject;

public class JsonFactory {

	private static JsonFactory instance = null;
	
	public static final String TYPE_ADD_NEARBY_PERSON = "add_nearby_person";

	public static final String TYPE_REMOVE_NEARBY_PERSON = "remove_nearby_person";

	public static final String TYPE_NEARBY_PERSON = "nearby_person";

	public static final String TYPE_ROOM_PERSON = "room_person";

	public static final String TYPE_CHAT_CONTENT = "chat_content";
	
	public static final String TYPE_INVITE_JOIN_ROOM = "invite_join_room";
	
	public static final String TYPE_JOIN_ROOM = "join_room";
	
	public static final String TYPE_QUIT_ROOM = "quit_room";
	
	public static final String TYPE_UPDATE_USER_INFO = "update_user_info";
	
	private static final String DATA = "data";
	
	private static final String TYPE = "type";
	
	private static final String KEY_NICK = "nick";
	
	private static final String KEY_SEX = "sex";
	
	private static final String KEY_ID = "id";
	
	private static final String KEY_ROOM_ID = "room_id";
	
	private static final String KEY_CONTENT = "content";
	
	private static final String KEY_LATITUDE = "latitude";
	
	private static final String KEY_LONGITUDE = "longitude";
	
	private static final String TYPE_LOGIN = "login";
	
	private static final String TYPE_CREATE_ROOM = "create_room";
	
	private JsonFactory() {};
	
	public static JsonFactory getFactory() {
		if (instance == null) {
			instance = new JsonFactory();
		}
		return instance;
	}

	/**
	 * 生成用户登录json
	 * 
	 * @param id
	 * @param lat
	 * @param lon
	 * @param nick
	 * @param sex
	 * @return
	 */
	public JSONObject makeUserLoginJson(int id, int lat,
			int lon, String nick, int sex) {
		JSONObject data = makeLocationJson(id, lat, lon);
		data.put(KEY_NICK, nick);
		data.put(KEY_SEX, sex);
		return makeCommonJson(TYPE_LOGIN, data);
	}
	
	public  JSONObject makeJoinRoomJson(int id, int roomId) {
		JSONObject data = new JSONObject();
		data.put(KEY_ID, id);
		data.put(KEY_ROOM_ID, roomId);
		return makeCommonJson(TYPE_JOIN_ROOM, data);
	}
	
	public  JSONObject makeCreateRoomJson(int id) {
		JSONObject data = new JSONObject();
		data.put(KEY_ID, id);
		return makeCommonJson(TYPE_CREATE_ROOM, data);
	}
	
	public  JSONObject makeSayContentJson(int id, String content, String nick, int sex) {
		JSONObject data = new JSONObject();
		data.put(KEY_ID, id);
		data.put(KEY_CONTENT, content);
		data.put(KEY_NICK, nick);
		data.put(KEY_SEX, sex);
		return makeCommonJson(TYPE_CHAT_CONTENT, data);
	}
	
	public  JSONObject makeQuitRoomJson(int id, int roomId) {
		JSONObject data = new JSONObject();
		data.put(KEY_ID, id);
		data.put(KEY_ROOM_ID, roomId);
		return makeCommonJson(TYPE_QUIT_ROOM, data);
	}
	
	public  JSONObject makeQueryRoomPersonJson(int id) {
		JSONObject data = new JSONObject();
		data.put(KEY_ID, id);
		return makeCommonJson(TYPE_ROOM_PERSON, data);
	}
	
	public  JSONObject makeUpdateInfoJson(int id, String newNick, int newSex) {
		JSONObject data = new JSONObject();
		data.put(KEY_ID, id);
		data.put(KEY_NICK, newNick);
		data.put(KEY_SEX, newSex);
		return makeCommonJson(TYPE_UPDATE_USER_INFO, data);
	} 

	/**
	 * 生成仅包含位置和ID的JSON
	 * 
	 * @param id
	 * @param lat
	 * @param lon
	 * @return
	 */
	private  JSONObject makeLocationJson(int id, int lat, int lon) {
		JSONObject data = new JSONObject();
		data.put(KEY_ID, id);
		data.put(KEY_LATITUDE, lat);
		data.put(KEY_LONGITUDE, lon);
		return data;
	}

	/**
	 * 生成常用Json，只有data和type两个属性的Json
	 * 
	 * @param type
	 *            消息类型
	 * @param data
	 *            消息数据
	 * @return
	 */
	private  JSONObject makeCommonJson(String type, Object data) {
		JSONObject result = new JSONObject();
		result.put(DATA, data);
		result.put(TYPE, type);
		return result;
	}
	
}
